home *** CD-ROM | disk | FTP | other *** search
/ IRIX 6.5 Applications 2004 April / SGI IRIX 6.5 Applications 2004 April.iso / dist / mozilla.idb / var / netscape / mozilla / chrome / toolkit.jar.z / toolkit.jar / content / global / printjoboptions.js < prev    next >
Text File  |  2003-11-11  |  16KB  |  461 lines

  1. /* -*- Mode: Java; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
  2. /* ***** BEGIN LICENSE BLOCK *****
  3.  * Version: NPL 1.1/GPL 2.0/LGPL 2.1
  4.  *
  5.  * The contents of this file are subject to the Netscape Public License
  6.  * Version 1.1 (the "License"); you may not use this file except in
  7.  * compliance with the License. You may obtain a copy of the License at
  8.  * http://www.mozilla.org/NPL/
  9.  *
  10.  * Software distributed under the License is distributed on an "AS IS" basis,
  11.  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
  12.  * for the specific language governing rights and limitations under the
  13.  * License.
  14.  *
  15.  * The Original Code is Mozilla Communicator client code.
  16.  *
  17.  * The Initial Developer of the Original Code is 
  18.  * Netscape Communications Corporation.
  19.  * Portions created by the Initial Developer are Copyright (C) 1998
  20.  * the Initial Developer. All Rights Reserved.
  21.  *
  22.  * Contributor(s): 
  23.  *   Masaki Katakai <katakai@japan.sun.com>
  24.  *   Roland Mainz <roland.mainz@informatik.med.uni-giessen.de>
  25.  *   Asko Tontti <atontti@cc.hut.fi>
  26.  *
  27.  * Alternatively, the contents of this file may be used under the terms of
  28.  * either the GNU General Public License Version 2 or later (the "GPL"), or 
  29.  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
  30.  * in which case the provisions of the GPL or the LGPL are applicable instead
  31.  * of those above. If you wish to allow use of your version of this file only
  32.  * under the terms of either the GPL or the LGPL, and not to allow others to
  33.  * use your version of this file under the terms of the NPL, indicate your
  34.  * decision by deleting the provisions above and replace them with the notice
  35.  * and other provisions required by the GPL or the LGPL. If you do not delete
  36.  * the provisions above, a recipient may use your version of this file under
  37.  * the terms of any one of the NPL, the GPL or the LGPL.
  38.  *
  39.  * ***** END LICENSE BLOCK ***** */
  40.  
  41. var dialog;
  42. var gPrintSettings = null;
  43. var gStringBundle  = null;
  44. var gPrintSettingsInterface  = Components.interfaces.nsIPrintSettings;
  45. var gPaperArray;
  46. var gPrefs;
  47.  
  48. var default_command    = "lpr";
  49. var gPrintSetInterface = Components.interfaces.nsIPrintSettings;
  50. var doDebug            = true;
  51.  
  52. //---------------------------------------------------
  53. function checkDouble(element, maxVal)
  54. {
  55.   var value = element.value;
  56.   if (value && value.length > 0) {
  57.     value = value.replace(/[^\.|^0-9]/g,"");
  58.     if (!value) {
  59.       element.value = "";
  60.     } else {
  61.       if (value > maxVal) {
  62.         element.value = maxVal;
  63.       } else {
  64.         element.value = value;
  65.       }
  66.     }
  67.   }
  68. }
  69.  
  70. //---------------------------------------------------
  71. function isListOfPrinterFeaturesAvailable()
  72. {
  73.   var has_printerfeatures = false;
  74.   
  75.   try {
  76.     has_printerfeatures = gPrefs.getBoolPref("print.tmp.printerfeatures." + gPrintSettings.printerName + ".has_special_printerfeatures");
  77.   } catch(ex) {
  78.   }
  79.   
  80.   return has_printerfeatures;
  81. }
  82.  
  83. //---------------------------------------------------
  84. function getDoubleStr(val, dec)
  85. {
  86.   var str = val.toString();
  87.   var inx = str.indexOf(".");
  88.   return str.substring(0, inx+dec+1);
  89. }
  90.  
  91. //---------------------------------------------------
  92. function initDialog()
  93. {
  94.   dialog = new Object;
  95.  
  96.   dialog.paperList       = document.getElementById("paperList");
  97.   dialog.paperGroup      = document.getElementById("paperGroup");
  98.   
  99.   dialog.cmdLabel        = document.getElementById("cmdLabel");
  100.   dialog.cmdInput        = document.getElementById("cmdInput");
  101.  
  102.   dialog.colorGroup      = document.getElementById("colorGroup");
  103.   dialog.colorRadio      = document.getElementById("colorRadio");
  104.   dialog.grayRadio       = document.getElementById("grayRadio");
  105.  
  106.   dialog.topInput        = document.getElementById("topInput");
  107.   dialog.bottomInput     = document.getElementById("bottomInput");
  108.   dialog.leftInput       = document.getElementById("leftInput");
  109.   dialog.rightInput      = document.getElementById("rightInput");
  110. }
  111.  
  112. //---------------------------------------------------
  113. function round10(val)
  114. {
  115.   return Math.round(val * 10) / 10;
  116. }
  117.  
  118.  
  119. //---------------------------------------------------
  120. function listElement(aListElement)
  121.   {
  122.     this.listElement = aListElement;
  123.   }
  124.  
  125. listElement.prototype =
  126.   {
  127.     clearList:
  128.       function ()
  129.         {
  130.           // remove the menupopup node child of the menulist.
  131.           this.listElement.removeChild(this.listElement.firstChild);
  132.         },
  133.  
  134.     appendPaperNames: 
  135.       function (aDataObject) 
  136.         { 
  137.           var popupNode = document.createElement("menupopup"); 
  138.           for (var i=0;i<aDataObject.length;i++)  {
  139.             var paperObj = aDataObject[i];
  140.             var itemNode = document.createElement("menuitem");
  141.             var label;
  142.             try {
  143.               label = gStringBundle.GetStringFromName(paperObj.name)
  144.             } 
  145.             catch (e) {
  146.               /* No name in string bundle ? Then build one manually (this
  147.                * usually happens when gPaperArray was build by createPaperArrayFromPrinterFeatures() ...) */              
  148.               if (paperObj.inches) {
  149.                 label = paperObj.name + " (" + round10(paperObj.width) + "x" + round10(paperObj.height) + " inch)";
  150.               }  
  151.               else {
  152.                 label = paperObj.name + " (" + paperObj.width + "x" + paperObj.height + " mm)";
  153.               }
  154.             }
  155.             itemNode.setAttribute("label", label);
  156.             itemNode.setAttribute("value", i);
  157.             popupNode.appendChild(itemNode);            
  158.           }
  159.           this.listElement.appendChild(popupNode); 
  160.         } 
  161.   };
  162.  
  163. //---------------------------------------------------
  164. function createPaperArrayFromDefaults()
  165. {
  166.   var paperNames   = ["letterSize", "legalSize", "exectiveSize", "a5Size", "a4Size", "a3Size", "a2Size", "a1Size", "a0Size"];
  167.   //var paperNames   = ["&letterRadio.label;", "&legalRadio.label;", "&exectiveRadio.label;", "&a4Radio.label;", "&a3Radio.label;"];
  168.   var paperWidths  = [ 8.5,  8.5,  7.25, 148.0, 210.0, 287.0, 420.0, 594.0,  841.0];
  169.   var paperHeights = [11.0, 14.0, 10.50, 210.0, 297.0, 420.0, 594.0, 841.0, 1189.0];
  170.   var paperInches  = [true, true, true,  false, false, false, false, false, false];
  171.   // this is deprecated
  172.   var paperEnums  = [0, 1, 2, 3, 4, 5, 6, 7, 8];
  173.  
  174.   gPaperArray = new Array();
  175.  
  176.   for (var i=0;i<paperNames.length;i++) {
  177.     var obj    = new Object();
  178.     obj.name   = paperNames[i];
  179.     obj.width  = paperWidths[i];
  180.     obj.height = paperHeights[i];
  181.     obj.inches = paperInches[i];
  182.     obj.paperSize = paperEnums[i]; // deprecated
  183.     
  184.     /* Calculate the width/height in millimeters */
  185.     if (paperInches[i]) {
  186.       obj.width_mm  = paperWidths[i]  * 25.4;
  187.       obj.height_mm = paperHeights[i] * 25.4; 
  188.     }
  189.     else {
  190.       obj.width_mm  = paperWidths[i];
  191.       obj.height_mm = paperHeights[i];        
  192.     }
  193.     gPaperArray[i] = obj;
  194.   }
  195. }
  196.  
  197. //---------------------------------------------------
  198. function createPaperArrayFromPrinterFeatures()
  199. {
  200.   var printername = gPrintSettings.printerName;
  201.   if (doDebug) {
  202.     dump("createPaperArrayFromPrinterFeatures for " + printername + ".\n");
  203.   }
  204.  
  205.   gPaperArray = new Array();
  206.   
  207.   var numPapers = gPrefs.getIntPref("print.tmp.printerfeatures." + printername + ".paper.count");
  208.   
  209.   if (doDebug) {
  210.     dump("processing " + numPapers + " entries...\n");
  211.   }    
  212.  
  213.   for (var i=0;i<numPapers;i++) {
  214.     var obj    = new Object();
  215.     obj.name      = gPrefs.getCharPref("print.tmp.printerfeatures." + printername + ".paper." + i + ".name");
  216.     obj.width_mm  = gPrefs.getIntPref("print.tmp.printerfeatures."  + printername + ".paper." + i + ".width_mm");
  217.     obj.height_mm = gPrefs.getIntPref("print.tmp.printerfeatures."  + printername + ".paper." + i + ".height_mm");
  218.     obj.inches    = gPrefs.getBoolPref("print.tmp.printerfeatures." + printername + ".paper." + i + ".is_inch");
  219.     obj.paperSize = 666; // deprecated
  220.     
  221.     /* Calculate the width/height in paper's native units (either inches or millimeters) */
  222.     if (obj.inches) {
  223.       obj.width  = obj.width_mm  / 25.4;
  224.       obj.height = obj.height_mm / 25.4; 
  225.     }
  226.     else {
  227.       obj.width  = obj.width_mm;
  228.       obj.height = obj.height_mm;
  229.     }
  230.  
  231.     gPaperArray[i] = obj;
  232.  
  233.     if (doDebug) {
  234.       dump("paper index=" + i + ", name=" + obj.name + ", width=" + obj.width + ", height=" + obj.height + ".\n");
  235.     }
  236.   }  
  237. }
  238.  
  239. //---------------------------------------------------
  240. function createPaperArray()
  241. {  
  242.   if (isListOfPrinterFeaturesAvailable()) {
  243.     createPaperArrayFromPrinterFeatures();    
  244.   }
  245.   else {
  246.     createPaperArrayFromDefaults();
  247.   }
  248. }
  249.  
  250. //---------------------------------------------------
  251. function createPaperSizeList(selectedInx)
  252. {
  253.   gStringBundle = srGetStrBundle("chrome://communicator/locale/printPageSetup.properties");
  254.  
  255.   var selectElement = new listElement(dialog.paperList);
  256.   selectElement.clearList();
  257.  
  258.   selectElement.appendPaperNames(gPaperArray);
  259.  
  260.   if (selectedInx > -1) {
  261.     selectElement.listElement.selectedIndex = selectedInx;
  262.   }
  263.  
  264.   //dialog.paperList = selectElement;
  265. }   
  266.  
  267. //---------------------------------------------------
  268. function loadDialog()
  269. {
  270.   var print_paper_type       = 0;
  271.   var print_paper_unit       = 0;
  272.   var print_paper_width      = 0.0;
  273.   var print_paper_height     = 0.0;
  274.   var print_paper_name       = "";
  275.   var print_color            = true;
  276.   var print_command          = default_command;
  277.  
  278.   gPrefs = Components.classes["@mozilla.org/preferences-service;1"].getService(Components.interfaces.nsIPrefBranch);
  279.  
  280.   if (gPrintSettings) {
  281.     print_paper_type   = gPrintSettings.paperSizeType;
  282.     print_paper_unit   = gPrintSettings.paperSizeUnit;
  283.     print_paper_width  = gPrintSettings.paperWidth;
  284.     print_paper_height = gPrintSettings.paperHeight;
  285.     print_paper_name   = gPrintSettings.paperName;
  286.     print_color        = gPrintSettings.printInColor;
  287.     print_command      = gPrintSettings.printCommand;
  288.   }
  289.  
  290.   if (doDebug) {
  291.     dump("loadDialog******************************\n");
  292.     dump("paperSizeType "+print_paper_unit+"\n");
  293.     dump("paperWidth    "+print_paper_width+"\n");
  294.     dump("paperHeight   "+print_paper_height+"\n");
  295.     dump("paperName     "+print_paper_name+"\n");
  296.     dump("printInColor  "+print_color+"\n");
  297.     dump("printCommand  "+print_command+"\n");
  298.   }
  299.  
  300.   createPaperArray();
  301.  
  302.   var selectedInx = 0;
  303.   for (var i=0;i<gPaperArray.length;i++) { 
  304.     if (print_paper_name == gPaperArray[i].name) {
  305.       selectedInx = i;
  306.       break;
  307.     }
  308.   }
  309.   
  310.   if (doDebug) {
  311.     if (i == gPaperArray.length)
  312.       dump("loadDialog: No paper found.\n");
  313.     else
  314.       dump("loadDialog: found paper '"+gPaperArray[selectedInx].name+"'.\n");
  315.   }
  316.  
  317.   createPaperSizeList(selectedInx);
  318.   
  319.   // Enable/disable widgets based in the information whether the selected
  320.   // printer supports the matching feature or not
  321.   if (isListOfPrinterFeaturesAvailable()) {
  322.     if (gPrefs.getBoolPref("print.tmp.printerfeatures." + gPrintSettings.printerName + ".can_change_spoolercommand"))
  323.       dialog.cmdInput.removeAttribute("disabled");
  324.     else
  325.       dialog.cmdInput.setAttribute("disabled","true");
  326.  
  327.     if (gPrefs.getBoolPref("print.tmp.printerfeatures." + gPrintSettings.printerName + ".can_change_paper_size"))
  328.       dialog.paperList.removeAttribute("disabled");
  329.     else
  330.       dialog.paperList.setAttribute("disabled","true");
  331.   }
  332.  
  333.   if (print_command == "") {
  334.     print_command = default_command;
  335.   }
  336.  
  337.   if (print_color) {
  338.     dialog.colorGroup.selectedItem = dialog.colorRadio;
  339.   } else {
  340.     dialog.colorGroup.selectedItem = dialog.grayRadio;
  341.   }
  342.  
  343.   dialog.cmdInput.value = print_command;
  344.  
  345.   /* First initalize with the hardcoded defaults... */
  346.   dialog.topInput.value    = "0.04";
  347.   dialog.bottomInput.value = "0.04";
  348.   dialog.leftInput.value   = "0.04";
  349.   dialog.rightInput.value  = "0.04";
  350.  
  351.   try {
  352.     /* ... then try to get the generic settings ... */
  353.     dialog.topInput.value    = gPrefs.getIntPref("print.print_edge_top") / 100.0;
  354.     dialog.bottomInput.value = gPrefs.getIntPref("print.print_edge_bottom") / 100.0;
  355.     dialog.leftInput.value   = gPrefs.getIntPref("print.print_edge_left") / 100.0;
  356.     dialog.rightInput.value  = gPrefs.getIntPref("print.print_edge_right") / 100.0;
  357.  
  358.     /* ... and then the printer specific settings. */
  359.     var printername = gPrintSettings.printerName;
  360.     dialog.topInput.value    = gPrefs.getIntPref("print.printer_"+printername+".print_edge_top") / 100.0;
  361.     dialog.bottomInput.value = gPrefs.getIntPref("print.printer_"+printername+".print_edge_bottom") / 100.0;
  362.     dialog.leftInput.value   = gPrefs.getIntPref("print.printer_"+printername+".print_edge_left") / 100.0;
  363.     dialog.rightInput.value  = gPrefs.getIntPref("print.printer_"+printername+".print_edge_right") / 100.0;
  364.   } catch (e) {  }
  365. }
  366.  
  367. //---------------------------------------------------
  368. function onLoad()
  369. {
  370.   // Init dialog.
  371.   initDialog();
  372.  
  373.   gPrintSettings = window.arguments[0].QueryInterface(gPrintSetInterface);
  374.   paramBlock = window.arguments[1].QueryInterface(Components.interfaces.nsIDialogParamBlock);
  375.  
  376.   if (doDebug) {
  377.     if (gPrintSettings == null) alert("PrintSettings is null!");
  378.     if (paramBlock == null) alert("nsIDialogParam is null!");
  379.   }
  380.  
  381.   // default return value is "cancel"
  382.   paramBlock.SetInt(0, 0);
  383.  
  384.   loadDialog();
  385. }
  386.  
  387. //---------------------------------------------------
  388. function onAccept()
  389. {
  390.   var print_paper_type   = gPrintSettingsInterface.kPaperSizeDefined;
  391.   var print_paper_unit   = gPrintSettingsInterface.kPaperSizeInches;
  392.   var print_paper_width  = 0.0;
  393.   var print_paper_height = 0.0;
  394.   var print_paper_name   = "";
  395.  
  396.   if (gPrintSettings != null) {
  397.     var selectedInx = dialog.paperList.selectedIndex;
  398.     if (gPaperArray[selectedInx].inches) {
  399.       print_paper_unit = gPrintSettingsInterface.kPaperSizeInches;
  400.     } else {
  401.       print_paper_unit = gPrintSettingsInterface.kPaperSizeMillimeters;
  402.     }
  403.     print_paper_width  = gPaperArray[selectedInx].width;
  404.     print_paper_height = gPaperArray[selectedInx].height;
  405.     print_paper_name   = gPaperArray[selectedInx].name;
  406.     gPrintSettings.paperSize = gPaperArray[selectedInx].paperSize; // deprecated
  407.  
  408.     gPrintSettings.paperSizeType = print_paper_type;
  409.     gPrintSettings.paperSizeUnit = print_paper_unit;
  410.     gPrintSettings.paperWidth    = print_paper_width;
  411.     gPrintSettings.paperHeight   = print_paper_height;
  412.     gPrintSettings.paperName     = print_paper_name;
  413.  
  414.     // save these out so they can be picked up by the device spec
  415.     gPrintSettings.printInColor = dialog.colorRadio.selected;
  416.     gPrintSettings.printCommand = dialog.cmdInput.value;
  417.  
  418.     // 
  419.     try {
  420.       var printerName = gPrintSettings.printerName;
  421.       var i = dialog.topInput.value * 100;
  422.       gPrefs.setIntPref("print.printer_"+printerName+".print_edge_top", i);
  423.  
  424.       i = dialog.bottomInput.value * 100;
  425.       gPrefs.setIntPref("print.printer_"+printerName+".print_edge_bottom", i);
  426.  
  427.       i = dialog.leftInput.value * 100;
  428.       gPrefs.setIntPref("print.printer_"+printerName+".print_edge_left", i);
  429.  
  430.       i = dialog.rightInput.value * 100;
  431.       gPrefs.setIntPref("print.printer_"+printerName+".print_edge_right", i);
  432.     } catch (e) {
  433.     }
  434.  
  435.     if (doDebug) {
  436.       dump("onAccept******************************\n");
  437.       dump("paperSize     "+gPrintSettings.paperSize+" (deprecated)\n");
  438.       dump("paperSizeType "+print_paper_type+" (should be 1)\n");
  439.       dump("paperSizeUnit "+print_paper_unit+"\n");
  440.       dump("paperWidth    "+print_paper_width+"\n");
  441.       dump("paperHeight   "+print_paper_height+"\n");
  442.       dump("paperName     '"+print_paper_name+"'\n");
  443.  
  444.       dump("printInColor  "+gPrintSettings.printInColor+"\n");
  445.       dump("printCommand  '"+gPrintSettings.printCommand+"'\n");
  446.     }
  447.   } else {
  448.     dump("************ onAccept gPrintSettings: "+gPrintSettings+"\n");
  449.   }
  450.  
  451.   if (paramBlock) {
  452.     // set return value to "ok"
  453.     paramBlock.SetInt(0, 1);
  454.   } else {
  455.     dump("*** FATAL ERROR: paramBlock missing\n");
  456.   }
  457.  
  458.   return true;
  459. }
  460.  
  461.